﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace NuaTarDoneng.Tail
{
    public static class SecurityMan
    {
        public static string GenerKey() {
            int q = 0;
            char[] bchar = new char[64];
            string bb = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
            char[] bbc = bb.ToCharArray();
            byte[] a = new byte[64];
            for (byte i = 0; i < 64; i++)
            {
                a[i] = i;
            }
            Random rd = new Random();
            byte k = 0;
            for (int j = 0; j < 64; j++)
            {
                do
                {
                    q++;
                    k = (byte)rd.Next(64);
                    //
                }
                while (k == j);
                byte mid = a[k];
                a[k] = a[j];
                a[j] = mid;
            }
            for (int j = 0; j < 64; j++)
            {
                bchar[j] = bbc[a[j]];
            }
            return new string(bchar);
        }
        public static string Encodes(string key,string sstr) {
            string b64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(sstr));
            char[] d64 = b64.ToCharArray();
            string bb = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
            char[] bbc = bb.ToCharArray();
            char[] keyc = key.ToCharArray();
            for (int j = 0; j < b64.Length; j++)
            {
                char d = d64[j];
                int an = bb.IndexOf(d);
                if (an >= 0)
                {
                    d64[j] = key[an];
                }
            }
            string cc = new string(d64);//经过加密的串！！
            return cc;

        }
        public static string Decodes(string key, string sstr)//sstr为加密后的串
        {
            char[] ddc = sstr.ToCharArray();
            char[] keyc = key.ToCharArray();
            string bb = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
            char[] bbc = bb.ToCharArray();
            for (int i = 0; i < ddc.Length; i++)
            {
                char d = ddc[i];
                int an = key.IndexOf(d);
                if (an >= 0)
                {
                    ddc[i] = bb[an];
                }
            }
            return new string(ddc);

        }
      
    }
}